package com.meizi.experimentcode.thread;

import lombok.SneakyThrows;

/**
 * @author Administration
 * @date 2022/4/14 - 19:28 - 星期四
 * @project lambda
 * @package com.meizi.experimentcode
 * @by 晨
 */
public class Race implements Runnable{

    private static String winner;

    @SneakyThrows
    @Override
    public void run() {
        for (int i = 0; i <= 100 ; i++) {

            if (Thread.currentThread().getName().equals("兔子") && i%10 == 0) {
                Thread.sleep(10);
            }

            boolean flag = gameOver(i);
            if (flag) {
                break;
            }

            System.out.println(Thread.currentThread().getName() + "->>>跑了"+i+"步");
        }
    }

    private boolean gameOver(int steps) {
        if (winner != null) {
            return true;
        } {
            if (steps >= 100) {
                winner = Thread.currentThread().getName();
                System.out.println("winner is " + winner);
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Race r1 = new Race();
        Race r2 = new Race();

        new Thread(r1,"兔子").start();
        new Thread(r2, "乌龟").start();
    }
}
